from matplotlib import pyplot as plt
import string
import random
import pickle

colors = {'r','g','b','y','k','c','m'}
marks  = {'o','D','h','H','.','p'}

def generate_hdr_battery(numhdr,numcols,generator,normalizer):
    """"Generates an HDR Battery
    
        Keyword arguments:
        numhdr     -- number of HDRs to be generated
        numcols    -- number of columns/dimensions per HDR
        generator  -- the HDRGenerator to be used (non-null)
        normalizer -- the HDRNormalizer to be used (non-null)
    """
    battery = []
    for x in range(numhdr):
        src = numcols if x == 0 else battery[x-1]
        battery.append(normalizer.normalize_hdr(generator.generate_hdr(src)))
        
    battery_dict = {}
    for (hdr,x) in zip(battery,range(len(battery))):
        battery_dict[x] = hdr #hdr_dict[''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(5))] = hdr
        
    return battery_dict

def save_battery_to_file(battery, filename):
    output = open(filename, 'wb')
    pickle.dump(battery, output)
    output.close()
    return

def load_battery_from_file(filename):
    pkl_file = open(filename, 'rb')
    battery = pickle.load(pkl_file)
    pkl_file.close()
    return battery

def plot_hdr_battery(battery):
    """Plot an HDR Battery onto a single Matplotlib figure"""
    label_gen = ("{0}{1}".format(c,m) for m in marks for c in colors)
    labels = [next(label_gen) for v in range(len(battery))]
    x_series = range(len(battery[0]))
    for x in range(len(battery)):
        #plt.plot(x_series,battery[x],labels[x])
        plt.plot(x_series,battery[x])
    plt.show()
    return
